package com.flyou.ccnucenter.ui.activity.account;

import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.flyou.ccnucenter.R;
import com.flyou.ccnucenter.domain.MyUser;
import com.flyou.ccnucenter.ui.activity.BaseActivity1;
import com.flyou.ccnucenter.utils.BmobErrorCodeUtils;
import com.flyou.ccnucenter.utils.SPUtils;
import com.flyou.ccnucenter.widget.ClearEditText;

import cn.bmob.v3.listener.UpdateListener;


public class ChangeEmailActivity extends BaseActivity1 implements View.OnClickListener {
    private ClearEditText mNewEmail;
    private RelativeLayout mTitlebarRlBack;
    private TextView mTitlebarTvTitle;
    private TextView mTitlebarTvTextRight;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_change_email);
        mTitlebarRlBack = (RelativeLayout) findViewById(R.id.titlebar_rl_back);
        mTitlebarTvTitle = (TextView) findViewById(R.id.titlebar_tv_title);
        mTitlebarTvTextRight = (TextView) findViewById(R.id.titlebar_tv_text_right);
        mNewEmail = (ClearEditText) findViewById(R.id.et_new_nickname);
        String email = (String) SPUtils.get(ChangeEmailActivity.this, "email", "");
        mNewEmail.setText(email);
    }

    @Override
    protected void initDate() {
        mTitlebarTvTitle.setText("设置邮箱");
        mTitlebarTvTextRight.setVisibility(View.VISIBLE);
        mTitlebarTvTextRight.setText("保存");
    }

    @Override
    protected void setListener() {
        mTitlebarRlBack.setOnClickListener(this);
        mTitlebarTvTextRight.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.titlebar_rl_back:
                finish();
                break;
            case R.id.titlebar_tv_text_right:
                final String email = mNewEmail.getText().toString().trim();
                if (email.isEmpty()) {
                    Toast.makeText(this, R.string.regex_email_empty, Toast.LENGTH_SHORT).show();
                    return;
                }
//                if (!RegexPatternUtils.isEmail(email)) {
//                    Toast.makeText(this, R.string.regex_email_error, Toast.LENGTH_SHORT).show();
//                    return;
//                }

                MyUser myUser = new MyUser();
                myUser.setEmail(email);
                MyUser bmobMyUser = MyUser.getCurrentUser(this, MyUser.class);
                myUser.update(this, bmobMyUser.getObjectId(), new UpdateListener() {
                    @Override
                    public void onSuccess() {
                        SPUtils.put(ChangeEmailActivity.this, "email", email);
                        Toast.makeText(ChangeEmailActivity.this, "修改邮箱成功", Toast.LENGTH_SHORT).show();
                        finish();
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        String message = BmobErrorCodeUtils.getMessage(code, msg);
                        Toast.makeText(ChangeEmailActivity.this, message, Toast.LENGTH_SHORT).show();
                    }
                });


                break;

        }
    }
}
